package by.amakarevich.services.impl;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.AccessoriesDao;
import by.amakarevich.dataaccess.ProductDao;
import by.amakarevich.datamodel.Accessories;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.AccessoriesService;

@Service
public class AccessoriesServiceImpl implements AccessoriesService {

	@Inject
	private AccessoriesDao accessoriesDao;

	@Inject
	private ProductDao productDao;

	@Override
	public void createNewAccessories(Accessories accessories, Product product) {
		Validate.isTrue(
				accessories.getId() == null,
				"This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		accessories.setProduct(product);
		accessoriesDao.insert(accessories);
	}

	@Override
	public Accessories get(Long id) {
		return accessoriesDao.getById(id);
	}

	@Override
	public void updateAccessories(Accessories accessories) {
		accessoriesDao.update(accessories);
	}

	@Override
	public void removeAccessories(Long id) {

		accessoriesDao.delete(id);
		productDao.delete(id);

	}

	@Override
	public void deleteAll() {
		accessoriesDao.deleteAll();
		productDao.deleteAll();
	}
}
